package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVO;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity selectById(Long id, @RequestHeader("Authorization") String token) {

        /*//1.校验token
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken) {
            return ResponseEntity.status(401).body(null);
        }
        //2.解析token
        Claims claims = JwtUtils.getClaims(token);
        Integer idd = (Integer) claims.get("id");*/
        if (id == null) {
            id = UserHolder.getUserId();
        }
        UserInfoVO userInfoVO = userInfoService.selectById(id);
        return ResponseEntity.ok(userInfoVO);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {

       /* //1.校验token
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken) {
            return ResponseEntity.status(401).body(null);
        }
        //2.解析token
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/

        Long id = UserHolder.getUserId();

        userInfo.setId(Long.valueOf(id));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }
}
